package com.shizhanzhe.zxbapp.customtask;

import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.CreditCardBean;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.widget.MyGridView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by YJ on 2018/10/23.
 */
public class CreditCardAdapter extends RecyclerView.Adapter<CreditCardAdapter.ViewHolder> {

    private Context mContext;
    private List<CreditCardBean.StepBean> mData = new ArrayList<>();
    private ImageClickListener mListener;

    public void setListener(ImageClickListener listener) {
        mListener = listener;
    }

    public CreditCardAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<CreditCardBean.StepBean> dataSet) {
        if (dataSet != null && !dataSet.isEmpty()) {
            mData = dataSet;
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_credit_card, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final CreditCardBean.StepBean dataBean = mData.get(position);
        if (position==0){
            holder.y.setVisibility(View.INVISIBLE);
        }
        if (position==mData.size()-1){
            holder.y2.setVisibility(View.INVISIBLE);
        }
        holder.tvStep.setText("第" + (position + 1) + "步");
        holder.tvStepName.setText(dataBean.getDescription());
        if (!"".equals(dataBean.getImgs())){
            holder.innerRecycler.setVisibility(View.VISIBLE);
            holder.innerRecycler.setAdapter(new ImageAdapter(mContext, Arrays.asList(dataBean.getImgs().split(","))));
        } else {
            holder.innerRecycler.setVisibility(View.GONE);
        }
        holder.tvStepName.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                ClipboardManager cm = (ClipboardManager)mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(holder.tvStepName.getText().toString());
                Toast.makeText(mContext, "文本已复制", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_step)
        TextView tvStep;
        @BindView(R.id.y)
        TextView y;
        @BindView(R.id.y2)
        TextView y2;
        @BindView(R.id.tv_step_name)
        TextView tvStepName;
        @BindView(R.id.gridView)
        MyGridView innerRecycler;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ImageAdapter extends BaseAdapter {
        private List<String> datas;
        private Context mContext;

        public ImageAdapter(Context mContext, List<String> datas) {
            this.mContext = mContext;
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int i) {
            return datas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder vh;
            if (view == null) {
                vh = new ViewHolder();
                view = LayoutInflater.from(mContext).inflate(R.layout.item_source_image, null);
                vh.ivImage = (ImageView) view.findViewById(R.id.iv_img);
                view.setTag(vh);
            } else {
                vh = (ViewHolder) view.getTag();
            }
            Glide.with(mContext).load(Path.BASE_URL + datas.get(i)).into(vh.ivImage);
            //vh.ivImage.setImageResource(R.mipmap.logo);
            vh.ivImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.imageClick(datas.get(i));
                }
            });
            return view;
        }

        class ViewHolder {
            public ImageView ivImage;
        }
    }

    public interface ImageClickListener {
        void imageClick(String url);
    }

}
